package com.eagle.scali.contentcenter.controller;

import com.eagle.scali.commoncenter.auth.CheckLogin;
import com.eagle.scali.commoncenter.util.JwtOperator;
import com.eagle.scali.contentcenter.dao.content.ShareMapper;
import com.eagle.scali.contentcenter.domain.dto.content.Notice;
import com.eagle.scali.contentcenter.domain.dto.content.ShareAuditDTO;
import com.eagle.scali.contentcenter.domain.dto.content.ShareDTO;
import com.eagle.scali.contentcenter.domain.dto.user.UserDTO;
import com.eagle.scali.contentcenter.domain.entity.content.Share;
import com.eagle.scali.contentcenter.service.content.ShareService;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shares")
@ComponentScan(basePackages = { "com.eagle.scali.commoncenter"})
public class ContentController {

	@Autowired(required = false)
	private ShareService shareService;

	@Autowired(required = false)
	private JwtOperator jwtOperator;

	/**
	 *	X-Token:eyJhbGciOiJIUzI1NiJ9.eyJpZCI6NCwid3hOaWNrbmFtZSI6IumjniIsInJvbGUiOiJhZG1pbiIsImlhdCI6MTU4NjI3MDEzMiwiZXhwIjoxNTg3NDc5NzMyfQ.Y-K9mxCgyvEQnqP-3pArAGfvcYnA_Ylkjj7P-Y7gr4g
	 *	http://localhost:8060/shares/1
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@CheckLogin
	public ShareDTO findById(@PathVariable Integer id) {
		return this.shareService.findById(id);
	}

	@GetMapping("/q")
	public PageInfo<Share> q(
			@RequestParam(required = false) String title,
			@RequestParam(required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize,
			@RequestHeader(value = "X-Token", required = false) String token) {
		if (pageSize > 100) {
			pageSize = 100;
		}
		Integer userId = null;
		if (StringUtils.isNotBlank(token)) {
			Claims claims = this.jwtOperator.getClaimsFromToken(token);
			userId = (Integer) claims.get("id");
		}
		return this.shareService.q(title, pageNo, pageSize, userId);
	}

	@GetMapping("/exchange/{id}")
	@CheckLogin
	public Share exchangeById(@PathVariable Integer id, HttpServletRequest request) {
		return this.shareService.exchangeById(id, request);
	}

	@GetMapping("/user")
	@CheckLogin
	public List<Share>  getExchangeBook(@RequestHeader(value = "X-Token", required = false) String token) {
		Claims claims = this.jwtOperator.getClaimsFromToken(token);
		Integer userId = (Integer) claims.get("id");
		return this.shareService.getExchangeBook(userId);
	}

	@GetMapping("/newest")
	public Notice  newest() {
		Notice notice = new Notice();
		notice.setContent("欢迎关注公众号，你可以分享你的读书经验，或者学习到别人的经验，更多精彩内容等着你");
		return notice;
	}

	@PostMapping("/contribute")
	@CheckLogin
	public Share contribute(@RequestHeader(value = "X-Token", required = false) String token,
							@RequestBody ShareDTO shareDTO) {
		Claims claims = this.jwtOperator.getClaimsFromToken(token);
		Integer userId = (Integer) claims.get("id");
		return this.shareService.contributeShare(userId, shareDTO);

	}

	@GetMapping("/preview/{id}")
	@CheckLogin
	public ShareDTO previewById(@RequestHeader(value = "X-Token", required = false) String token,
							 @PathVariable Integer id) {
		return this.shareService.findById(id);
	}

	@PostMapping("/findUsers")
	@CheckLogin
	public List<ShareDTO> findShareListByUserIds(@RequestBody List<String> userIds) {
		return this.shareService.findByUserId(userIds);
	}

}
